package graphs;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import error.GedivaException;

/**
 * displays a tag cloud of the given data
 * 
 * @author lindsaykubasik
 * 
 */
@SuppressWarnings("serial")
public class TagCloud extends AbstractGraph<String, Double>
{

    /**
     * Constructor for tag cloud
     */
    public TagCloud()
    {
        myData = null;
        myLabels = null;
        myXvals = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics pen)
    {
        Dimension canvas = getSize();
        pen.drawString(myLabels.get(0), (canvas.width / 2), 20);
        double yMax = 0;
        int k = 0, l = 0;
        double yVal = 0;
        for (Object x : myXvals)
        {
            if (!(x instanceof String)
                    || !(myData.get(x).get(0) instanceof Double))
                throw new GedivaException("Improper data types.");

            if (myData.get(x).get(0) instanceof Double)

                yVal = (Double) myData.get(x).get(0);

            if (yVal > yMax)
                yMax = yVal;
        }
        Dimension bounds = getSize();
        for (Object x : myXvals)
        {
            yVal = (Double) myData.get(x).get(0);
            double percent = yVal / yMax;
            int standard = 50;
            int fontSize = (int) (standard * percent);
            pen.setColor(randomColor());
            pen.setFont(new Font("Helvetica", Font.BOLD, fontSize));
            if (k * fontSize < bounds.width && (l * 20 + 50) < bounds.height)
                pen.drawString((String) x, (k * 100), (l * 20) + 50);
            k++;
            if (k * 25 > bounds.width)
            {
                l++;
                k = 0;
            }
            if ((l * 20) + 50 > bounds.height)
            {
                l = 0;
                k = 0;
            }

        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractGraph#toString()
     */
    public String toString()
    {
        return "TagCloud";
    }
}